package tech.aistar.common;

import org.apache.commons.jci.compilers.CompilationResult;
import org.apache.commons.jci.compilers.EclipseJavaCompiler;
import org.apache.commons.jci.compilers.EclipseJavaCompilerSettings;
import org.apache.commons.jci.problems.CompilationProblem;
import org.apache.commons.jci.readers.FileResourceReader;
import org.apache.commons.jci.stores.FileResourceStore;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class WebJavaComilper {
	private String version = "1.8";
	private String JavaPath ;
	private String javaName;
	
	public List<String> comilper() {
		File file = new File(JavaPath);
		List<String> result = new ArrayList<String>();
		EclipseJavaCompilerSettings setting = new EclipseJavaCompilerSettings();
		setting.setSourceVersion("1.7");
		setting.setTargetVersion("1.7");
		EclipseJavaCompiler javaCompiler = new EclipseJavaCompiler(setting);
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
        FileResourceReader reader = new FileResourceReader(file);
        FileResourceStore store = new FileResourceStore(file);
        CompilationResult cr = javaCompiler.compile(new String[] {javaName + ".java"}, 
        		reader, store,loader);
       CompilationProblem[] cp = cr.getErrors();
       for (CompilationProblem tmp : cp) {
    	   result.add(tmp.getMessage() +"："+tmp.getStartLine());
       }
       return result;
	}
	
	public String getVersion() {
		return version;
	}
	public void setVersion(String version) {
		this.version = version;
	}
	public String getJavaPath() {
		return JavaPath;
	}
	public void setJavaPath(String javaPath) {
		JavaPath = javaPath;
	}
	public String getJavaName() {
		return javaName;
	}
	public void setJavaName(String javaName) {
		this.javaName = javaName;
	}
	
}
